#!/bin/bash
set -x

s2z="${1:-textures.s2z}"
shift 1

if [ -z "${s2z}" ]
then
    1>&2 echo "usage:"
    1>&2 echo "  ${0} <textures.s2z> [<option>]"
    exit 1
fi

# get absolute path to s2z
s2z="$(realpath "${s2z}")"


if [ ! -d textures ]
then
  mkdir -p textures
  cd textures
  # 7zz x ../"${s2z}" 1>&2
  7zz x "${s2z}" 1>&2
  cd ..
fi

echo 'set -ex'
files textures | natsort | narrow dds$ | resub -m '^(textures/[0][0-9]+/)(.*?/)([^/]+)[.]dds$' 'mkdir -p "\2"' | natsort | uniq
files textures | natsort | narrow dds$ | resub -m '^(textures/[0][0-9]+/)(.*?)[.]dds$' 'convert "\1\2.dds" -flip "\2.tga"' | natsort -r

